layui.use(['form','tree','element','table','laytpl','treeGrid','layer','jquery'],function(){
    var form = layui.form,
        element = layui.element;
    $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var table = layui.table;
    var laytpl = layui.laytpl;
    var layedit = layui.layedit;

    //获取路径参数
    var request = GetRequest();

    $.get('/table/rows/list/' + request.tableName,function (data) {
        var col = [];
        col.push({type: "checkbox", fixed:"left", width:50});
        $.each(data.data,function (i, ele) {
            var obj = null;
            if (ele.formType == "img") {
                obj = {field: ''+ele.name, title: ele.nameZh, minWidth:100, align:"center", templet:function(d){
                        //处理时间
                        return '<img width="50" src="'+ d[ele.name] +'"/>';
                    }};
            } else if(ele.formType == "layedit") {
                return;
            } else {
                obj = {field: ''+ele.name, title: ele.nameZh, minWidth:100, align:"center"};
            }
            col.push(obj);
        });
        col.push({field: 'createDate', title: '记录时间', align:'center',minWidth:150,sort:true, templet:function(d){
                //处理时间
                var date = new Date(d.create_date);
                return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
            }});
        col.push({title: '操作', minWidth:200, templet:'#tableListBar',fixed:"right",align:"center"});

        //列表数据
        var option = {
            elem: '#tableList',
            url : '/tracetable/select?tableName='+ request.tableName +'&produceBatchId=' + request.id + '&processId=' + request.processId,
            cellMinWidth : 95,
            page : true,
            height : "full-125",
            limits : [10,15,20,25],
            limit : 15,
            id : "listTable",
            cols : [col]
        };

        //数据记录列表
        tableIns = table.render(option);
    });


    //搜索投入品记录信息
    $(".search_btn").on("click",function(){
        var searchKey = $(".searchVal").val();
        if(searchKey != ''){
            /*option.url = '/record/invest/query';
            option.method = 'post';
            option.where = {name: searchKey};
            table.render(option);*/

        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加投入品记录
    $(".addBtn").click(function(){
        var index = layui.layer.open({
            title : "添加记录信息",
            type : 2,
            content : "tracerecordAdd?id=" + request.id + '&processId=' + request.processId + '&tableName=' + request.tableName,
            area: ['100%',$(document).height() + 'px'],
            success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        });
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('listTable'),//此处的id为table数据格式中的id
            data = checkStatus.data,
            idArr = [];
        if(data.length > 0) {
            for (var i in data) {
                idArr.push(data[i].id);
            }
            layer.confirm('确定删除选中的投入品记录信息？', {icon: 3, title: '提示信息'}, function (index) {
                for(var i=0;i<idArr.length;i++){
                    $.get("/record/invest/delete?id=" + idArr[i],function(data){
                        if (data.code == 200) {
                            layer.msg("删除成功！");
                        } else {
                            layer.msg("删除失败，请重试！");
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                }
            })
        }else{
            layer.msg("请选择需要删除的投入品记录");
        }
    });

    //列表操作
    table.on('tool(tableList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            var index = layui.layer.open({
                title : "修改记录信息",
                type : 2,
                content : "tracerecordEdit?id=" + data.id + '&tableName=' + request.tableName + '&produceBatchId=' + request.id + '&processId=' + request.processId,
                area: ['100%',$(document).height() - 30 + 'px'],
                success : function(){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回投入品记录列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            });
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此条记录？',{icon:3, title:'提示信息'},function(index){
                $.get("/tracetable/delete?id=" + data.id + '&tableName=' + request.tableName,function(data){
                    if (data.code == 200) {
                        layer.msg("删除成功！");
                    } else {
                        layer.msg("删除失败，请重试！");
                    }
                    tableIns.reload();
                    layer.close(index);
                });
            });
        }
    });


    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});